<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="&amp;#x524D;&amp;#x63D0;&amp;#x200B;    &amp;#x597D;&amp;#x4E45;&amp;#x90FD;&amp;#x6CA1;&amp;#x66F4;&amp;#x65B0;&amp;#x4E86;&amp;#xFF0C;&amp;#x6700;&amp;#x8FD1;&amp;#x56E0;&amp;#x4E3A;&amp;#x770B;&amp;#x8BBE;&amp;#x8BA1;&amp;#x6A21;&amp;#x5F0F;&amp;#xFF0C;&amp;#x56E0;&amp;#x4E3A;&amp;#x9996;&amp;#x5">
<meta property="og:type" content="article">
<meta property="og:title" content="Android观察者模式初探">
<meta property="og:url" content="http://yoursite.com/2019/08/21/Android观察者模式初探/index.html">
<meta property="og:site_name" content="Silence潇湘夜雨">
<meta property="og:description" content="&amp;#x524D;&amp;#x63D0;&amp;#x200B;    &amp;#x597D;&amp;#x4E45;&amp;#x90FD;&amp;#x6CA1;&amp;#x66F4;&amp;#x65B0;&amp;#x4E86;&amp;#xFF0C;&amp;#x6700;&amp;#x8FD1;&amp;#x56E0;&amp;#x4E3A;&amp;#x770B;&amp;#x8BBE;&amp;#x8BA1;&amp;#x6A21;&amp;#x5F0F;&amp;#xFF0C;&amp;#x56E0;&amp;#x4E3A;&amp;#x9996;&amp;#x5">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1716569-116cd2cc32a17f6b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-07-19T05:31:00.539Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android观察者模式初探">
<meta name="twitter:description" content="&amp;#x524D;&amp;#x63D0;&amp;#x200B;    &amp;#x597D;&amp;#x4E45;&amp;#x90FD;&amp;#x6CA1;&amp;#x66F4;&amp;#x65B0;&amp;#x4E86;&amp;#xFF0C;&amp;#x6700;&amp;#x8FD1;&amp;#x56E0;&amp;#x4E3A;&amp;#x770B;&amp;#x8BBE;&amp;#x8BA1;&amp;#x6A21;&amp;#x5F0F;&amp;#xFF0C;&amp;#x56E0;&amp;#x4E3A;&amp;#x9996;&amp;#x5">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/1716569-116cd2cc32a17f6b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/08/21/Android观察者模式初探/">





  <title>Android观察者模式初探 | Silence潇湘夜雨</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Silence潇湘夜雨</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Android开发一枚</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home //首页"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user //关于"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags  //标签"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th //分类"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive //归档"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar //日程表"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap //站点地图"></i> <br>
            
            站点地图
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/08/21/Android观察者模式初探/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Silence潇湘夜雨">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Silence潇湘夜雨">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android观察者模式初探</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-21T15:51:43+08:00">
                2019-08-21
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index">
                    <span itemprop="name">Android</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  960
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="&#x524D;&#x63D0;"><a href="#&#x524D;&#x63D0;" class="headerlink" title="&#x524D;&#x63D0;"></a>&#x524D;&#x63D0;</h3><p>&#x200B;    &#x597D;&#x4E45;&#x90FD;&#x6CA1;&#x66F4;&#x65B0;&#x4E86;&#xFF0C;&#x6700;&#x8FD1;&#x56E0;&#x4E3A;&#x770B;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;&#xFF0C;&#x56E0;&#x4E3A;&#x9996;&#x5148;&#x770B;&#x7684;&#x662F;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#x3002;&#x5BF9;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#x6709;&#x4E86;&#x4E00;&#x4E2A;&#x6BD4;&#x8F83;&#x5168;&#x9762;&#x7684;&#x7406;&#x89E3;&#x3002;&#x4ECA;&#x5929;&#x6597;&#x80C6;&#x6765;&#x8BF4;&#x4E00;&#x4E0B;&#x81EA;&#x5DF1;&#x7684;&#x7406;&#x89E3;&#xFF0C;&#x8FD8;&#x671B;&#x5404;&#x4F4D;&#x770B;&#x5B98;&#x8001;&#x7237;&#x8F7B;&#x70B9;&#x6253;&#x8138;&#x3002;</p>
<p>###&#x8BDD;&#x4E0D;&#x591A;&#x8BF4;&#x5148;&#x770B;&#x56FE;</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1716569-116cd2cc32a17f6b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;.png"></p>
<p>   _ &#x5BB9;&#x5C0F;&#x53EF;&#x89E3;&#x91CA;&#x4E00;&#x4E0B;,&#x5728;&#x8FD9;&#x4E2A;&#x754C;&#x9762;&#x4E2D;&#xFF0C;&#x8981;&#x5B9E;&#x73B0;&#x5728;&#x4E00;&#x4E2A;&#x8F93;&#x5165;&#x6846;&#x4E2D;&#x5199;&#x5165;&#x6587;&#x672C;&#x7136;&#x540E;&#x70B9;&#x51FB;&#x8BBE;&#x7F6E;&#x6309;&#x94AE;&#xFF0C;&#x5B9E;&#x73B0;&#x4E0B;&#x9762;&#x7684;TextView&#x5B9E;&#x65F6;&#x6539;&#x53D8;&#x7684;&#x529F;&#x80FD;&#x3002;&#x5230;&#x8FD9;&#x91CC;&#x4E5F;&#x8BB8;&#x6709;&#x4EBA;&#x4F1A;&#x95EE;&#x4E86;&#x53EF;&#x4EE5;&#x7528;TextWatcher&#x5B9E;&#x73B0;&#x554A;&#xFF0C;&#x5BF9;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;TextWatcher&#x4E2D;&#x8FD0;&#x7528;&#x7684;&#x5C31;&#x662F;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#xFF08;&#x5176;&#x5B9E;Android&#x4E2D;&#x8FD0;&#x7528;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#x7684;&#x5730;&#x65B9;&#x5F88;&#x591A;&#xFF09;&#x6765;&#x770B;&#x6B63;&#x6587;&#x3002;_</p>
<p>#&#x91CA;&#x4E49;</p>
<p>###&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;<br>&#x200B;      &#x5B9A;&#x4E49;&#x5BF9;&#x8C61;&#x95F4;&#x7684;&#x4E00;&#x79CD;&#x4E00;&#x4E2A;(Subject)&#x5BF9;&#x591A;(Observer)&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x53D1;&#x751F;&#x6539;&#x53D8;&#x65F6;&#xFF0C;&#x6240;&#x6709;&#x4F9D;&#x8D56;&#x4E8E;&#x5B83;&#x7684; &#x5BF9;&#x8C61;&#x90FD;&#x5F97;&#x5230;&#x901A;&#x77E5;&#x5E76;&#x88AB;&#x81EA;&#x52A8;&#x66F4;&#x65B0; &#x3002;</p>
<p>###&#x9002;&#x7528;&#x6027;&#xFF1A;<br>&#x200B;      1. &#x5F53;&#x4E00;&#x4E2A;&#x62BD;&#x8C61;&#x6A21;&#x578B;&#x6709;&#x4E24;&#x4E2A;&#x65B9;&#x9762;&#xFF0C;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x65B9;&#x9762;&#x4F9D;&#x8D56;&#x4E8E;&#x53E6;&#x4E00;&#x65B9;&#x9762; &#x5C06;&#x8FD9;&#x4E24;&#x8005;&#x5C01;&#x88C5;&#x6210;&#x72EC;&#x7ACB;&#x7684;&#x5BF9;&#x8C61;&#x4E2D;&#x4EE5;&#x4F7F;&#x5B83;&#x4EEC;&#x53EF;&#x4EE5;&#x5404;&#x81EA;&#x72EC;&#x7ACB;&#x7684;&#x6539;&#x53D8;&#x548C;&#x670D;&#x7528; &#x3002;<br>&#x200B;      2. &#x5F53;&#x5BF9;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x6539;&#x53D8;&#x9700;&#x8981;&#x540C;&#x65F6;&#x6539;&#x53D8;&#x5176;&#x4ED6;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x77E5;&#x9053;&#x5177;&#x4F53;&#x6709;&#x591A;&#x5C11;&#x5BF9;&#x8C61;&#x6709;&#x5F85;&#x6539;&#x53D8; &#x3002;<br>&#x200B;      3. &#x5F53;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x5FC5;&#x987B;&#x901A;&#x77E5;&#x5176;&#x5B83;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x5B83;&#x53C8;&#x4E0D;&#x80FD;&#x5047;&#x5B9A;&#x5176;&#x5B83;&#x5BF9;&#x8C61;&#x662F;&#x8C01; </p>
<h3 id="&#x53C2;&#x4E0E;&#x8005;&#xFF1A;"><a href="#&#x53C2;&#x4E0E;&#x8005;&#xFF1A;" class="headerlink" title="&#x53C2;&#x4E0E;&#x8005;&#xFF1A;"></a>&#x53C2;&#x4E0E;&#x8005;&#xFF1A;</h3><pre><code>  1. Subject&#xFF08;&#x76EE;&#x6807;&#xFF09; 
     &#x76EE;&#x6807;&#x77E5;&#x9053;&#x5B83;&#x7684;&#x89C2;&#x5BDF;&#x8005;&#xFF0C;&#x53EF;&#x4EE5;&#x6709;&#x4EFB;&#x610F;&#x591A;&#x4E2A;&#x89C2;&#x5BDF;&#x8005;&#x89C2;&#x5BDF;&#x540C;&#x4E00;&#x4E2A;&#x76EE;&#x6807;&#x63D0;&#x4F9B;&#x6CE8;&#x518C;&#x548C;&#x5220;&#x9664;&#x89C2;&#x5BDF;&#x8005;&#x5BF9;&#x8C61;&#x7684;&#x63A5;&#x53E3; 
  2. Observer&#xFF08;&#x89C2;&#x5BDF;&#x8005;&#xFF09; 
     &#x4E3A;&#x90A3;&#x4E9B;&#x5728;&#x76EE;&#x6807;&#x53D1;&#x751F;&#x6539;&#x53D8;&#x65F6;&#x9700;&#x83B7;&#x5F97;&#x901A;&#x77E5;&#x7684;&#x5BF9;&#x8C61;&#x5B9A;&#x4E49;&#x4E2A;&#x66F4;&#x65B0;&#x7684;&#x63A5;&#x53E3; 
  3. ConcreteSubject&#xFF08;&#x5177;&#x4F53;&#x76EE;&#x6807;&#xFF09; 
     &#x5C06;&#x6709;&#x5173;&#x72B6;&#x6001;&#x5B58;&#x5165;&#x5404;ConcreteObserver&#x5BF9;&#x8C61; &#x5F53;&#x5B83;&#x7684;&#x72B6;&#x6001;&#x53D1;&#x9001;&#x6539;&#x53D8;&#x65F6;&#xFF0C;&#x5411;&#x5B83;&#x7684;&#x5404;&#x4E2A;&#x89C2;&#x5BDF;&#x8005;&#x53D1;&#x51FA;&#x901A;&#x77E5; 
  4. ConcreteObserver&#xFF08;&#x5177;&#x4F53;&#x89C2;&#x5BDF;&#x8005;&#xFF09; 
&#x7EF4;&#x62A4;&#x4E00;&#x4E2A;&#x6307;&#x5411;ConcreteObserver&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x5B58;&#x50A8;&#x6709;&#x5173;&#x72B6;&#x6001;&#xFF0C;&#x8FD9;&#x4E9B;&#x72B6;&#x6001;&#x5E94;&#x4E0E;&#x76EE;&#x6807;&#x7684;&#x72B6;&#x6001;&#x4FDD;&#x6301;&#x4E00;&#x81F4; &#x5B9E;&#x73B0;Observer&#x7684;&#x66F4;&#x65B0;&#x63A5;&#x53E3;&#x662F;&#x81EA;&#x8EAB;&#x72B6;&#x6001;&#x4E0E;&#x76EE;&#x6807;&#x7684;&#x72B6;&#x6001;&#x4FDD;&#x6301;&#x4E00;&#x81F4; </code></pre><p>#&#x5B9E;&#x4F8B;<br>&#x200B;    &#x8BF4;&#x4E86;&#x90A3;&#x4E48;&#x591A;&#x7684;&#x540D;&#x8BCD;&#x89E3;&#x91CA;&#xFF0C;&#x4F30;&#x8BA1;&#x5404;&#x4F4D;&#x770B;&#x5B98;&#x90FD;&#x6709;&#x70B9;&#x8499;&#x5708;&#x4E86;&#xFF0C;&#x522B;&#x7740;&#x6025;&#xFF0C;&#x8BA9;&#x672C;&#x4EBA;&#x7528;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x7ED9;&#x4F60;&#x4EEC;&#x6F14;&#x793A;&#x4E00;&#x756A;&#xFF0C;&#x76F8;&#x4FE1;&#x806A;&#x660E;&#x7684;&#x4F60;&#x4EEC;&#x4E00;&#x5B9A;&#x4F1A;&#x770B;&#x61C2;&#x7684;&#x3002;</p>
<h4 id="1&#x3001;&#x9996;&#x5148;&#x662F;&#x89C2;&#x5BDF;&#x8005;Observer"><a href="#1&#x3001;&#x9996;&#x5148;&#x662F;&#x89C2;&#x5BDF;&#x8005;Observer" class="headerlink" title="1&#x3001;&#x9996;&#x5148;&#x662F;&#x89C2;&#x5BDF;&#x8005;Observer"></a>1&#x3001;&#x9996;&#x5148;&#x662F;&#x89C2;&#x5BDF;&#x8005;Observer</h4><pre><code>public class MyObserver implements Observer {

private Handler mHandler;
private String name;
public MyObserver() {
}

public MyObserver(Handler handler, String name) {
    this.mHandler = handler;
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public void update(Observable observable, Object data) {
    sendMessage(MsgBox.MSG_UPDATE_SUCCESS, data);
}
   /**
    *&#x53D1;&#x9001;&#x6D88;&#x606F;
    */
private void sendMessage(int what, Object object) {
    Message msg = new Message();
    msg.what = what;
    msg.obj = object;
    mHandler.sendMessage(msg);
}
}</code></pre><h4 id="2&#x3001;&#x88AB;&#x89C2;&#x5BDF;&#x8005;Observable"><a href="#2&#x3001;&#x88AB;&#x89C2;&#x5BDF;&#x8005;Observable" class="headerlink" title="2&#x3001;&#x88AB;&#x89C2;&#x5BDF;&#x8005;Observable"></a>2&#x3001;&#x88AB;&#x89C2;&#x5BDF;&#x8005;Observable</h4><pre><code> /**
 * &#x88AB;&#x89C2;&#x5BDF;&#x8005;
  * 
  * @author Silence
  * 
 */
 public class MyObserable extends Observable {

public void postNewPublication(String content) {
    setChanged();
    notifyObservers(content);
}
}</code></pre><h4 id="3&#x3001;&#x5728;Activity&#x4E2D;&#x4F7F;&#x7528;"><a href="#3&#x3001;&#x5728;Activity&#x4E2D;&#x4F7F;&#x7528;" class="headerlink" title="3&#x3001;&#x5728;Activity&#x4E2D;&#x4F7F;&#x7528;"></a>3&#x3001;&#x5728;Activity&#x4E2D;&#x4F7F;&#x7528;</h4><pre><code> public class SecondActivity extends BaseActivity {
 // &#x89C2;&#x5BDF;&#x8005;
 private Button mBtnGet, mBtnSetting;
 private TextView mTxtName;
 private EditText mEdtContent;
 // &#x89C2;&#x5BDF;&#x8005;
 private MyObserver mObserver;
 // &#x88AB;&#x89C2;&#x5BDF;&#x8005;
 MyObserable obserable = new MyObserable();

@Override
protected void setContentView() {
    setContentView(R.layout.activity_second);
}

@Override
protected void initView() {
    mBtnGet = $(R.id.btn_get);
    mBtnSetting = $(R.id.btn_setting);
    mTxtName = $(R.id.txt_name);
    mEdtContent = $(R.id.edt_content);

}

@Override
protected void initData() {

}

@Override
protected void setListener() {
    mBtnGet.setOnClickListener(this);
    mBtnSetting.setOnClickListener(this);
}

@Override
protected void initLitener(View view) {
    switch (view.getId()) {
    case R.id.btn_get: {
        getData();
    }

        break;
    case R.id.btn_setting: {
        setData();
    }

        break;

    default:
        break;
    }
}

/**
 * &#x8BBE;&#x7F6E;&#x6570;&#x636E;
 */
protected void setData() {
    String content = mEdtContent.getText().toString().trim();
    mObserver = new MyObserver(mHandler, content);
    obserable.addObserver(mObserver);
    obserable.postNewPublication(content);
}

/**
 * &#x83B7;&#x53D6;&#x6570;&#x636E;
 */
protected void getData() {

    startActivity(new Intent(SecondActivity.this, ThirdActivity.class));
}

/**
 * Handler
 */
Handler mHandler = new Handler() {
    @Override
    public void handleMessage(android.os.Message msg) {
        switch (msg.what) {
        case MsgBox.MSG_UPDATE_SUCCESS:
            String name = (String) msg.obj;
            mTxtName.setText(name);
            break;

        default:
            break;
        }
    };
};

@Override
protected void onDestroy() {
    super.onDestroy();
    obserable.deleteObserver(mObserver);
}

}</code></pre><p>#&#x611F;&#x8C22;<br><a href="http://blog.csdn.net/fangchongbory/article/details/7774044" target="_blank" rel="noopener">Android&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;&#x4E4B;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;</a><br><a href="http://www.jianshu.com/p/316147958b67" target="_blank" rel="noopener">Android&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;&#xFF08;&#x5341;&#x4E00;&#xFF09;-&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/08/21/Android-工具类/" rel="next" title="Android 工具类">
                <i class="fa fa-chevron-left"></i> Android 工具类
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/08/21/Android-自定义Toast，并且勘误Android工具类里面的ToastUtils/" rel="prev" title="Android 自定义Toast，并且勘误Android工具类里面的ToastUtils">
                Android 自定义Toast，并且勘误Android工具类里面的ToastUtils <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Silence潇湘夜雨</p>
              <p class="site-description motion-element" itemprop="description">Android开发一枚</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/muyishuangfeng" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/1e2eec6f972c" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/58df0abf61ff4b006b115e43" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-star"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yangke20792452" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前提"><span class="nav-number">1.</span> <span class="nav-text">前提</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参与者："><span class="nav-number">2.</span> <span class="nav-text">参与者：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1、首先是观察者Observer"><span class="nav-number">2.1.</span> <span class="nav-text">1、首先是观察者Observer</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2、被观察者Observable"><span class="nav-number">2.2.</span> <span class="nav-text">2、被观察者Observable</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3、在Activity中使用"><span class="nav-number">2.3.</span> <span class="nav-text">3、在Activity中使用</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Silence潇湘夜雨</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">114.8k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

  <!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618},"display":{"superSample":2,"width":150,"height":300,"position":"right","hOffset":0,"vOffset":-20},"mobile":{"show":false},"react":{"opacityDefault":0.5,"opacityOnHover":0.5},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
